package com.tsing.common.utils;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateTimeUtil {

    /**
     * 获取当前时间戳
     */
    public static Long getTimestampNow() {
        return System.currentTimeMillis();
    }

    /**
     * 获取当前时间
     */
    public static LocalDateTime getLocalDateTimeNow() {
        return LocalDateTime.now();
    }

    /**
     * 获取当前时间字符串
     */
    public static String getStrNow() {
        return DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now());
    }

    /**
     * 时间戳转时间
     */
    public static LocalDateTime getLocalDateTimeByTimestamp(long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    }

    /**
     * 时间转时间戳
     */
    public static Long getTimestampByLocalDateTime(LocalDateTime localDateTime) {
        Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
        return instant.toEpochMilli();
    }

    /**
     * 时间转字符串
     */
    public static String getStrByLocalDateTimeAndFormat(LocalDateTime localDateTime, String format) {
        return localDateTime.format(DateTimeFormatter.ofPattern(format));
    }

    /**
     * 字符串转时间
     */
    public static LocalDateTime getLocalDateTimeByStrAndFormat(String str, String format) {
        return LocalDateTime.parse(str, DateTimeFormatter.ofPattern(format));
    }

    /**
     * 时间戳转换日期(Date)
     */
    public static String stampToDate(long timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        return sdf.format(new Date(timestamp));

    }

    /**
     * 时间戳转日期时间(DateTime)
     */
    public static String stampToDateTime(long timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        return sdf.format(new Date(timestamp));
    }

}
